<template>
  <div class="app-container">
    <!--form 组件-->
    <eForm ref="form" :is-add="isAdd" :dicts="dict.user_status"/>
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :xs="9" :sm="6" :md="4" :lg="4" :xl="4">
        <div class="head-container">
          <el-input v-model="deptName" clearable placeholder="输入部门名称搜索" prefix-icon="el-icon-search"
                    style="width: 100%;" class="filter-item" @input="getDeptDatas"/>
        </div>
        <el-tree :data="depts" :props="defaultProps" :expand-on-click-node="false" default-expand-all
                 @node-click="handleNodeClick"/>
      </el-col>
      <!--用户数据-->
      <el-col :xs="15" :sm="18" :md="20" :lg="20" :xl="20">
        <!--工具栏-->
        <div class="head-container">
          <!-- 搜索 -->
          <el-input v-model="query.blurry" clearable placeholder="输入名称或者邮箱搜索" style="width: 200px;" class="filter-item"
                    @keyup.enter.native="toQuery"/>
          <el-date-picker
            v-model="query.date"
            type="daterange"
            range-separator=":"
            class="el-range-editor--small filter-item"
            style="height: 30.5px;width: 220px"
            value-format="yyyy-MM-dd HH:mm:ss"
            start-placeholder="开始日期"
            end-placeholder="结束日期"/>
          <el-select v-model="query.enabled" clearable placeholder="状态" class="filter-item" style="width: 90px"
                     @change="toQuery">
            <el-option v-for="item in enabledTypeOptions" :key="item.key" :label="item.display_name" :value="item.key"/>
          </el-select>
          <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toQuery">搜索
          </el-button>
          <!-- 新增 -->
          <div v-permission="['admin','user:add']" style="display: inline-block;margin: 0px 2px;">
            <el-button
              class="filter-item"
              size="mini"
              type="primary"
              icon="el-icon-plus"
              @click="add">新增
            </el-button>
          </div>
          <!-- 导出 -->
          <div style="display: inline-block;">
            <el-button
              :loading="downloadLoading"
              size="mini"
              class="filter-item"
              type="warning"
              icon="el-icon-download"
              @click="download">导出
            </el-button>
          </div>
        </div>
        <!--表格渲染-->
        <el-table v-loading="loading" :data="data" size="small" style="width: 100%;">
          <el-table-column prop="id" label="用户名id"/>
          <el-table-column prop="username" label="用户名"/>
          <el-table-column prop="phone" label="电话"/>
          <el-table-column :show-overflow-tooltip="true" prop="email" label="邮箱"/>
          <el-table-column label="部门">
            <template slot-scope="scope">
              <div>{{ scope.row.dept.name }}</div>
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.enabled"
                active-color="#409EFF"
                inactive-color="#F56C6C"
                @change="changeEnabled(scope.row, scope.row.enabled,)"/>
            </template>
          </el-table-column>
          <el-table-column :show-overflow-tooltip="true" prop="createTime" label="创建日期">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="checkPermission(['admin','user:edit','user:del'])" label="操作" width="200"
                           align="center" fixed="right">
            <template slot-scope="scope">
              <el-button v-permission="['admin','user:edit']" size="mini" type="primary" icon="el-icon-edit"
                         @click="edit(scope.row)"/>

              <el-popover
                v-permission="['admin','user:resetPass']"
                :ref="scope.row.id+'1'"
                placement="top"
                width="180">
                <p>确定重置用户密码吗？</p>
                <div style="text-align: right; margin: 0">
                  <el-button size="mini" type="text" @click="$refs[scope.row.id+'1'].doClose()">取消</el-button>
                  <el-button type="primary" size="mini" @click="resetPass(scope.row.id)">确定
                  </el-button>
                </div>
                <el-button slot="reference" size="mini" type="primary" icon="el-icon-setting"/>
              </el-popover>

              <el-popover
                v-permission="['admin','user:del']"
                :ref="scope.row.id"
                placement="top"
                width="180">
                <p>确定删除本条数据吗？</p>
                <div style="text-align: right; margin: 0">
                  <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
                  <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定
                  </el-button>
                </div>
                <el-button :disabled="scope.row.id===1" slot="reference" type="danger" icon="el-icon-delete"
                           size="mini"/>
              </el-popover>
            </template>
          </el-table-column>
        </el-table>
        <!--分页组件-->
        <el-pagination
          :total="total"
          :current-page="page + 1"
          style="margin-top: 8px;"
          layout="total, prev, pager, next, sizes"
          @size-change="sizeChange"
          @current-change="pageChange"/>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import checkPermission from '@/utils/permission'
  import initData from '@/mixins/initData'
  import {del, downloadUser, resetPass, edit, authInfo} from '@/api/user'
  import {getDepts} from '@/api/dept'
  import {parseTime, downloadFile} from '@/utils/index'
  import eForm from './form'

  export default {
    name: 'User',
    components: {eForm},
    mixins: [initData],
    // 设置数据字典
    dicts: ['user_status'],
    data() {
      return {
        height: document.documentElement.clientHeight - 180 + 'px;', isAdd: false,
        delLoading: false, deptName: '', depts: [], deptId: null,
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        enabledTypeOptions: [
          {key: 'true', display_name: '激活'},
          {key: 'false', display_name: '锁定'}
        ]
      }
    },
    created() {
      this.getDeptDatas()
      this.$nextTick(() => {
        this.init()
      })
    },
    mounted: function () {
      const that = this
      window.onresize = function temp() {
        that.height = document.documentElement.clientHeight - 180 + 'px;'
      }
    },
    methods: {
      parseTime,
      checkPermission,
      beforeInit() {
        this.url = 'api/users'
        const sort = 'id,desc'
        const query = this.query
        const blurry = query.blurry
        const enabled = query.enabled
        this.params = {page: this.page, size: this.size, sort: sort, deptId: this.deptId}
        if (blurry) {
          this.params['blurry'] = blurry
        }
        if (query.date) {
          this.params['startTime'] = query.date[0]
          this.params['endTime'] = query.date[1]
        }
        if (enabled !== '' && enabled !== null) {
          this.params['enabled'] = enabled
        }
        return true
      },
      // 导出
      download() {
        this.beforeInit()
        this.downloadLoading = true
        downloadUser(this.params).then(result => {
          downloadFile(result, '用户列表', 'xlsx')
          this.downloadLoading = false
        }).catch(() => {
          this.downloadLoading = false
        })
      },
      subDelete(id) {
        this.delLoading = true
        del(id).then(res => {
          this.delLoading = false
          this.$refs[id].doClose()
          this.dleChangePage()
          this.init()
          this.$notify({
            title: '删除成功',
            type: 'success',
            duration: 2500
          })
        }).catch(err => {
          this.delLoading = false
          this.$refs[id].doClose()
          console.log(err.response.data.message)
        })
      },
      getDeptDatas() {
        const sort = 'id,desc'
        const params = {sort: sort}
        if (this.deptName) {
          params['name'] = this.deptName
        }
        getDepts(params).then(res => {
          this.depts = res.content
        })
      },
      handleNodeClick(data) {
        if (data.pid === 0) {
          this.deptId = null
        } else {
          this.deptId = data.id
        }
        this.init()
      },
      add() {
        this.isAdd = true
        this.$refs.form.getDepts()
        this.$refs.form.getRoles()
        this.$refs.form.getRoleLevel()
        this.$refs.form.dialog = true
        this.$refs.form.isMerchant = false
      },
      // 数据转换
      formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => {
          if (j === 'createTime' || j === 'lastPasswordResetTime') {
            return parseTime(v[j])
          } else if (j === 'enabled') {
            return parseTime(v[j]) ? '启用' : '禁用'
          } else {
            return v[j]
          }
        }))
      },
      resetPass(id) {
        resetPass(id).then(res => {
          this.$refs[id + '1'].doClose()
        }).catch(e => {
          this.$refs[id + '1'].doClose()
        })
      },
      edit(data) {
        this.isAdd = false
        const _this = this.$refs.form
        _this.getRoles()
        _this.getDepts()
        _this.getRoleLevel()
        _this.roleIds = []
        _this.form = {
          id: data.id,
          username: data.username,
          phone: data.phone,
          email: data.email,
          enabled: data.enabled.toString(),
          roles: [],
          dept: {id: data.dept.id},
          frontCenterId: '',
          brandId: '',
          centerId: '',
          merchantList: data.merchantList ? JSON.parse(data.merchantList) : [],
          merchantId: data.merchantId,
        }
        data.roles.forEach(function (data, index) {
          _this.roleIds.push(data.id)
        })
        //判断是否为商户
        if (data.merchantList && data.merchantId) {
          _this.isMerchant = true
        }
        _this.deptId = data.dept.id
        _this.dialog = true
      },
      // 改变状态
      changeEnabled(data, val) {
        this.$confirm('此操作将 "' + this.dict.label.user_status[val] + '" ' + data.username + ', 是否继续？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          edit(data).then(res => {
            this.$notify({
              title: this.dict.label.user_status[val] + '成功',
              type: 'success',
              duration: 2500
            })
          }).catch(err => {
            data.enabled = !data.enabled
            console.log(err.response.data.message)
          })
        }).catch(() => {
          data.enabled = !data.enabled
        })
      }
    }
  }
</script>

<style scoped>
</style>
